/**
 * 佣金管理
 */
define(function(require, exports, module) {
    var $ = require("jquery");
    var angular = require("angular");
    require('authorize');
    require("permission");
    require("angular-route");
    require("angular-ui");
    require("angular-interceptor");
    require("./entrust-pay-service");
    require('global');

    var app = angular.module('app', ['ngRoute', 'ngUI', 'ngInterceptor', 'mainModule']);

    app.config(['$routeProvider', function ($routeProvider) {
      $routeProvider
          .when('/1/:brokerageState', { templateUrl: 'views/not-approve.html', controller: 'ViewController' })
          .when('/2/:brokerageState', { templateUrl: 'views/not-offer.html', controller: 'ViewController' })
          .when('/3/:brokerageState', { templateUrl: 'views/offer.html', controller: 'ViewController' })
          .otherwise({ redirectTo: '/1/1' });
    }]);

    function TabsController($scope, baseData, search, query) {
        $scope.baseData = baseData;
        // 初始化数据
        baseData.currentQueryService = search;
        setTimeout(function () {
            // setTimeout 为了让路由先完成，防止 dom 元素闪动
            $scope.$apply(function() {
                baseData.initData();
            });
        }, 0);

        $scope.changeTab = function (brokerageState) {
            if (brokerageState !== undefined) {
                baseData.brokerageState = brokerageState;
            }

            query.data = [];
            baseData.query();
        };
    }
    app.controller('TabsController', ['$scope', 'baseData', 'search', 'query', TabsController]);

    function ViewController($scope, $routeParams, query, search, filter, baseData, toolbar) {
        baseData.brokerageState = parseInt($routeParams.brokerageState);

        $scope.baseData = baseData;
        $scope.query = query;
        $scope.search = search;
        $scope.filter = filter;
        $scope.toolbar = toolbar;
    }

    app.controller('ViewController', ['$scope', '$routeParams', 'query', 'search', 'filter', 'baseData', 'toolbar', ViewController]);

    angular.bootstrap(document.getElementById('ng-app'), ['app']);
});